#pragma once

#include <pthread.h>

class Job;
class ThreadPool;
class ThreadWaiter;


class Thread
{
public:
    Thread(bool bDetach = true)
        : m_bDetach(bDetach), m_pThreadPool(NULL), m_pThreadWaiter(NULL)
    {
    }
    virtual ~Thread(void)
    {
    }

    virtual void Run() = 0;

    int Start();
    //int Join(pthread_t tid);
    //int Cancel(pthread_t tid);
    int Join();
    int Cancel();
    pthread_t Self();
    void Terminate();

    bool IsEqual(pthread_t tid);
    bool IsDetach();

    virtual void SetThreadPool(ThreadPool* pThreadPool);
    virtual void SetThreadWaiter(ThreadWaiter* pThreadWaiter);
    virtual Job* GetJob() { return NULL; };
    virtual void SetJob(Job* pJob) {};

protected:
    static void* ThreadFunc(void* ptr);

protected:
    pthread_t           m_tid;
    bool                m_bDetach;
    int                 m_nErrorCode;
    ThreadPool*         m_pThreadPool;
    ThreadWaiter*       m_pThreadWaiter;
};


